const app = getApp()
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileID: "",
    avatar: "",
    sex: "",
    value: "",
    signature: "",
    telephone: "",
    nickName: "",
    school:"",
    isPhone: false,
    isModal: false,
    userInfo: [],
    isSex: false,
    isNickname: false,
    isSchool: false
  },

  // 修改电话号码
  changePhone(){
    this.setData({
      isPhone: true,
      isModal: true
    })
  },

  //修改昵称
  changeName(){
    this.setData({
      isNickname: true,
      isModal: true
    })
  },

  //修改学校
  changeSchool(){
    this.setData({
      isSchool: true,
      isModal:true
    })
  },

  getAvatar(){
    // console.log(111)
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success:(res)=>{
        console.log(res)
        const tempFilePaths = res.tempFilePaths[0]
        this.setData({
          avatar:tempFilePaths
        })
        this.changePhoto()
      }
    })
  },

  //修改头像
  changePhoto(){
    const cloudPath = "avatars/" + app.userInfo._openid + Date.now() + ".png"
    wx.cloud.uploadFile({
      // cloudPath: "avaters/" + app.userInfo._openid + Date.now() + ".png",
      cloudPath,
      filePath: this.data.avatar, // 文件路径
    }).then(res => {
      console.log(res)
      const fileID = res.fileID
      this.setData({
        fileID
      })
      // app.userInfo.avatar = fileID
})
  },
  //完成模态框选择
  ensure(event){
    console.log(event)
    this.setData({
      isPhone: false,
      isNickname: false,
      isModal: false,
      isSchool: false
    })
  },

  //输入内容触发
  onInput(event){
    console.log(event)
    const value = event.detail.value
    this.setData({
      value
    })
  },
  onSignature(event){
    const signature = event.detail.value
    this.setData({
      signature
    })
  },

  //点击修改按钮
  enchange(){
    const value = this.data.value
    this.setData({
      telephone: value,
      isPhone: false,
      isModal:false
    })
  },
  enchangeName(){
    const value = this.data.value
    this.setData({
      nickName: value,
      isNickname: false,
      isModal:false
    })
  },
  enchangeSchool(){
    const value = this.data.value
    this.setData({
      school: value,
      isSchool: false,
      isModal: false
    })
  },

  //修改性别
  changeSex(){
    wx.showActionSheet({
      itemList: ['男', '女'],
      success:(res)=>{
        if(res.tapIndex == 0){
        this.setData({
          sex: "男"
        })
        }else{
          this.setData({
            sex: "女"
          })
        }
      }
    })
  },

  //点击保存
  save(){
    wx.showLoading({
      title: '正在保存',
    })
    db.collection('news').doc(app.userInfo._id).update({
      data: {
        avatar: this.data.fileID,
        nickName: this.data.nickName,
        sex: this.data.sex,
        telephone: this.data.telephone,
        signature: this.data.signature,
        school: this.data.school
      }
    }).then((res)=>{
      wx.hideLoading()
      wx.showToast({
        title: '保存成功',
      })
    })
    app.userInfo.avatar = this.data.avatar
    app.userInfo.nickName =  this.data.nickName
    app.userInfo.sex = this.data.sex
    app.userInfo.telephone = this.data.telephone
    app.userInfo.signature = this.data.signature
    app.userInfo.school = this.data.school
    setTimeout(()=>{
      wx.reLaunch({
        url: '/pages/mine/mine',
      })
    },2000)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.userInfo)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const userInfo = app.userInfo
    console.log(userInfo)
    this.setData({
      avatar: userInfo.avatar,
      fileID: userInfo.avatar,
      nickName: userInfo.nickName,
      sex: userInfo.sex,
      telephone: userInfo.telephone,
      signature: userInfo.signature,
      school: userInfo.school
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})